package com.iemes.controller.craft;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EquipApplyFormMap;
import com.iemes.entity.ErpEquipmentapply;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.TechnoParameterFormMap;
import com.iemes.entity.TechnoSettingFormMap;
import com.iemes.entity.TechnologyFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.craft.TechnoParameterMapper;
import com.iemes.mapper.craft.TechnoSettingMapper;
import com.iemes.mapper.craft.TechnologyMapper;
import com.iemes.plugin.PageView;
import com.iemes.upload.UploadUtil;
import com.iemes.util.Common;

@Controller
@RequestMapping("techno")
public class TechnologyController extends BaseController {
	@Inject
	private TechnologyMapper technologyMapper;
	@Inject
	private TechnoParameterMapper parameterMapper;
	@Inject 
	private TechnoSettingMapper settingMaper;
	
	@RequestMapping(value = "expanding", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/technology/pretest/expanding_list";
	}

	@ResponseBody
	@RequestMapping("expanding_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		TechnologyFormMap technologyFormMap = getFormMap(TechnologyFormMap.class);
		if (technologyFormMap.get("Material_Name")!=null) {
			technologyFormMap.put("Material_Name", "%"+technologyFormMap.get("Material_Name")+"%");
		}
		if (technologyFormMap.get("Unit_Weight")!=null) {
			technologyFormMap.put("Unit_Weight", "%"+technologyFormMap.get("Unit_Weight")+"%");
		}
		if (technologyFormMap.get("Material_Type")!=null) {
			technologyFormMap.put("Material_Type", "%"+technologyFormMap.get("Material_Type")+"%");
		}
		technologyFormMap.put("status", 1);
		technologyFormMap.put("orderby", "order by Enactment_Date");
		technologyFormMap = toFormMap(technologyFormMap, pageNow, pageSize,
				technologyFormMap.getStr("orderby"));
		List<TechnologyFormMap> findByPage = technologyMapper
				.findByPage(technologyFormMap);
		pageView.setRecords(findByPage);
		return pageView;
	}
	
	@RequestMapping("expanding_add")
	public String addUI(Model model, HttpServletRequest request)
			throws Exception {		
		return Common.BACKGROUND_PATH + "/technology/pretest/expanding_addUpd";
	}

	@ResponseBody
	@RequestMapping("expanding_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "预发工艺", methods = "预发工艺-新增工艺")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		TechnologyFormMap technologyFormMap = getFormMap(TechnologyFormMap.class);
		TechnoParameterFormMap technoParameterFormMap =getFormMap(TechnoParameterFormMap.class);
		TechnoSettingFormMap technoSettingFormMap = getFormMap(TechnoSettingFormMap.class);
		if (!Common.isNotEmpty(technologyFormMap.toString())||!Common.isNotEmpty(technoParameterFormMap.toString())||!Common.isNotEmpty(technoSettingFormMap.toString())) {
			return "false";
		}
		try {
			
			technoParameterFormMap.put("status", 1);
			technoSettingFormMap.put("status", 1);
			parameterMapper.addEntity(technoParameterFormMap);
			settingMaper.addEntity(technoSettingFormMap);
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			technologyFormMap.put("status", 1);
			technologyFormMap.put("date", sdf.format(new Date()));
			technologyFormMap.put("parameterId", technoParameterFormMap.get("id"));
			technologyFormMap.put("settingId", technoSettingFormMap.get("id"));
			technologyMapper.addEntity(technologyFormMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "false";
		}
		return "success";
	}

	// 查看工艺
	@RequestMapping(value = "expanding_edit")
	public String craft_edit(Model model) {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			TechnologyFormMap technology = technologyMapper.findbyFrist("id", id, TechnologyFormMap.class);
			TechnoParameterFormMap parameter = parameterMapper.findbyFrist("id", technology.get("parameterId").toString(), TechnoParameterFormMap.class);
			TechnoSettingFormMap setting = settingMaper.findbyFrist("id", technology.get("settingId").toString(), TechnoSettingFormMap.class);
			model.addAttribute("technology", technology);
			model.addAttribute("parameter", parameter);
			model.addAttribute("setting", setting);
		}
		if (Integer.valueOf(getPara("status")) == 1) {
			return Common.BACKGROUND_PATH + "/technology/pretest/expanding_editUpd";
		} else {
			return Common.BACKGROUND_PATH + "/technology/pretest/expanding_edit";
		}
	}
	
	//修改工艺操作
	@ResponseBody
	@RequestMapping("expanding_editEntity")
	@SystemLog(module="预发工艺",methods="预发工艺-修改工艺")//凡需要处理业务逻辑的.都需要记录操作日志
	@Transactional
	public String editEntity() throws Exception {
		//修改工艺为添加最新工艺，保存原来工艺，即为新增工艺，保存日期不一样
		TechnologyFormMap technologyFormMap = getFormMap(TechnologyFormMap.class);
		TechnoParameterFormMap technoParameterFormMap =getFormMap(TechnoParameterFormMap.class);
		TechnoSettingFormMap technoSettingFormMap = getFormMap(TechnoSettingFormMap.class);
		TechnoParameterFormMap technoParameter = new TechnoParameterFormMap();
		TechnoSettingFormMap technoSetting = new TechnoSettingFormMap();	
		try {
			//新增		
			technoSettingFormMap.put("status", 1);
			settingMaper.addEntity(technoSettingFormMap);
			technoParameterFormMap.put("status", 1);
			parameterMapper.addEntity(technoParameterFormMap);
			//修改原来工艺表
			technoParameter.put("id", technologyFormMap.get("parameterId"));
			technoParameter.put("status", 2);
			parameterMapper.editEntity(technoParameter);
			technoSetting.put("id", technologyFormMap.get("settingId"));
			technoSetting.put("status", 2);
			settingMaper.editEntity(technoSetting);		
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			technologyFormMap.put("date", sdf.format(new Date()));
			technologyFormMap.put("parameterId", technoParameterFormMap.get("id"));
			technologyFormMap.put("settingId",technoSettingFormMap.get("id"));
			technologyMapper.editEntity(technologyFormMap);
		} catch (Exception e) {
			return "false";
		}
		
		return "success";
	}
	//删除工艺操作
	@ResponseBody
	@RequestMapping("craft_deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="预发管理",methods="预发管理-删除工艺")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() {
		TechnoParameterFormMap technoParameter = new TechnoParameterFormMap();
		TechnoSettingFormMap technoSetting = new TechnoSettingFormMap();
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();

		for (int i = 0; i < str.length; i++) {
			//先查询子表的ID，修改子表后再修改父表
			try {
				TechnologyFormMap technologyFormMap = technologyMapper.findbyFrist("id", str[i], TechnologyFormMap.class);
				technoParameter.put("status", 2);
				technoParameter.put("id", technologyFormMap.get("parameterId"));
				technoSetting.put("status", 2);
				technoSetting.put("id", technologyFormMap.get("settingId"));
				parameterMapper.editEntity(technoParameter);
				settingMaper.editEntity(technoSetting);
				technologyFormMap.put("status", 2);
				technologyFormMap.put("deta", sdf.format(date));
				technologyMapper.editEntity(technologyFormMap);
				technologyFormMap.clear();
				technoParameter.clear();
				technoSetting.clear();
			} catch (Exception e) {
				return "false";
			}				
		}
			return "success";
	}

}
